package com.sjzl.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.sjzl.entity.Board;
import com.sjzl.entity.Post;
import com.sjzl.entity.PostVisitor;
import com.sjzl.entity.Reply;
import com.sjzl.entity.UploadFile;
import com.sjzl.entity.User;
import com.sjzl.service.BoardService;
import com.sjzl.service.PostService;
import com.sjzl.service.PostVisitorService;
import com.sjzl.service.ReplyService;
import com.sjzl.service.UploadFileService;
import com.sjzl.util.Pagination;
import com.sjzl.util.Warning;

@Component("replyAction")
@Scope("prototype")
public class ReplyAction extends BaseAction{
	
	@Resource(name="postServiceImpl")
	private PostService postService;
	
	@Resource(name="replyServiceImpl")
	private ReplyService replyService;
	
	@Resource(name="boardServiceImpl")
	private BoardService boardService;
	
	@Resource(name="uploadFileServiceImpl")
	private UploadFileService uploadFileService;
	
	@Resource(name="postVisitorServiceImpl")
	private PostVisitorService pvService;
	
	private Pagination pagination;
	public Pagination getPagination() {
		return pagination;
	}
	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	private List<Reply> replyList;
	public List<Reply> getReplyList() {
		return replyList;
	}
	public void setReplyList(List<Reply> replyList) {
		this.replyList = replyList;
	}
	
	private Integer post_id;
	public Integer getPost_id() {
		return post_id;
	}
	public void setPost_id(Integer post_id) {
		this.post_id = post_id;
	}
	
	private Post post;
	public Post getPost() {
		return post;
	}
	public void setPost(Post post) {
		this.post = post;
	}
	
	private List<Board> boardNavList;
	public List<Board> getBoardNavList() {
		return boardNavList;
	}
	
	private Reply reply;
	public Reply getReply() {
		return reply;
	}
	public void setReply(Reply reply) {
		this.reply = reply;
	}
	
	private Integer reply_id;
	public Integer getReply_id() {
		return reply_id;
	}
	public void setReply_id(Integer reply_id) {
		this.reply_id = reply_id;
	}
	
	private List<PostVisitor> todayVisitors;
	public List<PostVisitor> getTodayVisitors() {
		return todayVisitors;
	}
	public void setTodayVisitors(List<PostVisitor> todayVisitors) {
		this.todayVisitors = todayVisitors;
	}
	
	private List<UploadFile> uploadFiles;
	public List<UploadFile> getUploadFiles() {
		return uploadFiles;
	}
	public void setUploadFiles(List<UploadFile> uploadFiles) {
		this.uploadFiles = uploadFiles;
	}	
	
	private Warning warning;
	public Warning getWarning() {
		return warning;
	}
	
	@Transactional
	public String list(){
		post = postService.get(Post.class, post_id);
		totalCount = replyService.getTotalCount("Reply", "post = " + post_id);
		pagination = new Pagination(pageNo, 5, totalCount, "reply_list?post_id=" + post_id);
		replyList = replyService.pageDate("from Reply r where r.post = " + post_id, pageNo, 5);
		boardNavList = boardService.getBoardNav(post.getBoard().getId());
		List<UploadFile> uploads = uploadFileService.loadDownFile(post_id);//获取附件
		if(uploads.size()!=0||uploads.isEmpty()==false){//如果附件不为空才存到uploadFiles
			uploadFiles=uploadFileService.loadDownFile(post_id);
		}

		User sessionUser = (User) httpSession.getAttribute("sessionUser");
		String userAccount = null;
		if(sessionUser != null){
			userAccount = sessionUser.getAccount();
		}
		pvService.saveOrUpdateVisitors(userAccount, post.getId());//记录来访者
		todayVisitors = pvService.getTodayVisitors(post.getId());
		
		//更新查看总数
		Integer selectCount = post.getSelectCount();
		post.setSelectCount(selectCount+1);
		postService.update(post);
		this.setTitle(post.getName());
		return "reply_list";
	}
	
	public String create(){
		replyService.create(reply, post_id);
		return "create_success";
	}
	
	public String deleteReply(){
		return null;
	}
	
	public String createRReply(){
		System.out.println("reply_id="+reply_id);
		replyService.createRReply(reply, reply_id);
		return "createRReply_success";
	}
	
	public String createRReplyUI(){
		reply = replyService.get(Reply.class, reply_id);
		return "createRReplyUI";
	}
	
	/**
	 * 举报主题帖子
	 * @return
	 */
	public String report(){
		if(postService.report(post_id)){
			warning = new Warning("您成功举报了标题为：("+postService.get(Post.class, post_id).getName()+")的帖子");
			return list();
		}
		return null;
	}
	
	
}
